package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/18.
 * 我的订单 、交易明细
 */

public class OrderDealActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)//返回
    LinearLayout retur;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    @Bind(R.id.transaction_details_layout)//交易
    RelativeLayout transactionDetailsLayout;

    @Bind(R.id.transaction_details_img)
    ImageView transactionDetailsImg;

    @Bind(R.id.my_order_layout)//我的订单
    RelativeLayout myOrderLayout;

    @Bind(R.id.my_order_img)
    ImageView myOrderImg;

    LocalActivityManager manager = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_deal);
        ButterKnife.bind(this);
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);
        initView();
    }

    private void initView() {

        // 关闭预加载，默认一次只加载一个Fragment
        final ArrayList<View> list = new ArrayList<View>();
        Intent intent = new Intent(this,
                MyOrderActivity.class);
        list.add(getView("MyOrderActivity", intent));
        Intent intent2 = new Intent(this, TransactionDetailsActivity.class);
        list.add(getView("TransactionDetailsActivity", intent2));
        viewPager.setAdapter(new MyPagerAdapter(list));
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        viewPager.setCurrentItem(0);
        myOrderLayout.setOnClickListener(new MyOnClickListener(0));
        transactionDetailsLayout.setOnClickListener(new MyOnClickListener(1));
        retur.setOnClickListener(this);
    }
    /**
     * 通过activity获取视图
     *
     * @param id
     * @param intent
     * @return
     */
    private View getView(String id, Intent intent) {
        return manager.startActivity(id, intent).getDecorView();
    }
    /**
     * Pager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        List<View> list = new ArrayList<View>();

        public MyPagerAdapter(ArrayList<View> list) {
            this.list = list;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ViewPager pViewPager = ((ViewPager) container);
            pViewPager.removeView(list.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ViewPager pViewPager = ((ViewPager) arg0);
            pViewPager.addView(list.get(arg1));
            return list.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }
    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {


        @Override
        public void onPageSelected(int arg0) {


            switch (arg0) {
                case 0:
                    myOrderImg.setVisibility(View.VISIBLE);
                    transactionDetailsImg.setVisibility(View.GONE);
                    break;
                case 1:
                    myOrderImg.setVisibility(View.GONE);
                    transactionDetailsImg.setVisibility(View.VISIBLE);
                    break;
            }

        }
        @Override
        public void onPageScrollStateChanged(int pos) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    }
    /**
     2      * 头标点击监听
     3 */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }
}
